<template>
  <div class="add-repair-page">
    <h2>新增报修信息</h2>
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="报修单号" prop="repairNumber">
        <el-input 
          v-model="form.repairNumber"
          placeholder="待生成" 
          readonly
          :style="{ backgroundColor: '#f5f7fa', color: '#ccc' }">
        </el-input>
      </el-form-item>
      <el-form-item label="楼号" prop="buildingNumber">
        <el-input v-model="form.buildingNumber" placeholder="请输入楼号"></el-input>
      </el-form-item>
      <el-form-item label="报修地址" prop="repairAddress">
        <el-input v-model="form.repairAddress" placeholder="请输入报修地址"></el-input>
      </el-form-item>
      <el-form-item label="报修人" prop="reporter">
        <el-input v-model="form.reporter" placeholder="请输入报修人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="repairType">
        <el-select placeholder="请选择类型" style="width: 200px; margin-right: 10px" v-model="form.repairType">
          <el-option v-for="item in repairTypeList" :key="item.id" :value="item.type" :label="item.type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报修内容" prop="repairContent">
        <el-input v-model="form.repairContent" type="textarea" placeholder="请输入报修内容"></el-input>
      </el-form-item>
      <el-form-item label="报修图像">
        <el-upload
          class="avatar-uploader"
          :action="$baseUrl + '/files/upload'"
          :headers="{ token: user.token }"
          list-type="picture"
          :on-success="handleAvatarSuccess"
        >
          <el-button type="primary">上传报修图像</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save" :disabled="isSaving">保存</el-button>
        <el-button @click="resetForm">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AddRepair",
  data() {
    return {
      form: {
        pictures: []
      },
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        buildingNumber: [{ required: true, message: '请输入楼号', trigger: 'blur' }],
        repairAddress: [{ required: true, message: '请输入报修地址', trigger: 'blur' }],
        reporter: [{ required: true, message: '请输入报修人', trigger: 'blur' }],
        contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        repairType: [{ required: true, message: '请输入类型', trigger: 'blur' }],
        repairContent: [{ required: true, message: '请输入报修内容', trigger: 'blur' }]
      },
      repairTypeList: [],
      isSaving: false, // 新增提交状态控制
    };
  },
  computed: {
    isMobile() {
      return window.innerWidth < 768;
    }
  },
  created() {
    this.fetchRepairTypes();
  },
  methods: {
    fetchRepairTypes() {
      this.$request.get('/category/selectAll').then(res => {
        this.repairTypeList = res.data || [];
      });
    },
    save() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.isSaving = true
          this.$request({
            url: '/user/repair/add',
            method: 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功');
              this.resetForm();
            } else {
              this.$message.error(res.msg);
            }
          });
          this.isSaving = false
        }
      });
    },
    handleAvatarSuccess(response, file, fileList) {
      if (Array.isArray(this.form.pictures)) {
        this.form.pictures.push(response.data);
      } else {
        console.error('this.form.picture is not an array!');
        this.form.pictures = [response.data];
      }
    },
    resetForm() {
      this.form = {
        pictures: []
      };
      this.$refs.formRef.resetFields();
    }
  }
};
</script>

<style scoped>
.add-repair-page {
  padding: 20px;
}

/* 手机端样式适配 */
@media (max-width: 767px) {
  .add-repair-page {
    padding: 10px;
  }
  .el-form-item {
    width: 100%;
  }
  .el-select {
    width: 100%!important;
  }
}
</style>